<!DOCTYPE html>
<html xmlns:th="http://www.springframework.org/schema/jdbc">
<head>
    <meta charset="utf-8">
    <title>添加部门</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="/css/public.css" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <div class="layui-form layuimini-form">
            <div class="layui-form-item">
                <label class="layui-form-label required">部门名称</label>
                <div class="layui-input-block">
                    <input type="text" id="name" name="name" lay-verify="required" lay-reqtext="部门名称不能为空" placeholder="请输入部门名称"
                           value="" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item" id="DepartmentView"></div>
            <script id="DepartmentTemplate" type="text/html">
                <label class="layui-form-label" required>{{ d.label }}</label>
                <div class="layui-input-block" style="height: 38px;">
                    <select id="parent" name="parent" lay-verify="required" lay-search>
                        <option value="0">选择上级部门</option>
                        {{# layui.each(d.list, function(index, item){ }}
                        <option value="{{item.id}}">{{ item.name }}</option>
                        {{# }); }}
                    </select>
                </div>
            </script>
            <div class="layui-form-item">
                <label class="layui-form-label required">部门地点</label>
                <div class="layui-input-block">
                    <input type="text" id="address" name="address" lay-verify="required" lay-reqtext="部门地点不能为空"
                           placeholder="请输入部门地点" value="" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item layui-form-text">
                <label class="layui-form-label">部门描述</label>
                <div class="layui-input-block">
                    <textarea id="situation" name="situation" class="layui-textarea" placeholder="请输入单位描述"></textarea>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label required">负责人</label>
                <div class="layui-input-block">
                    <input type="text" id="principal" name="principal" lay-verify="required" lay-reqtext="负责人不能为空" placeholder="请输入负责人"
                           value="" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label required">联系电话</label>
                <div class="layui-input-block">
                    <input type="text" id="telnum" name="telnum" lay-verify="required" lay-reqtext="电话不能为空" placeholder="请输入联系电话"
                           value="" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn" lay-submit lay-filter="saveBtn">确认保存</button>
                    <button type="button" class="layui-btn layui-btn-primary" onclick="back()">取消</button>
                </div>
            </div>
        </div>
        <blockquote class="layui-elem-quote layui-text">
            注：请确保本页面数据正确性，本页信息将会关联到后期修改会影响人员结构关系。
        </blockquote>
    </div>
</div>
<script src="/lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script src="/js/jquery-3.2.1.js" charset="utf-8"></script>
<script th:inline="javascript">
    layui.use(['form', 'laytpl', 'layer'], function () {
        let form = layui.form,
            layer = layui.layer,
            laytpl = layui.laytpl;

        /**
         * 封装一个select选择器: selectTemplate(data, templateId, viewId);
         * 参数 :
         * 数据：data
         * 名称：name
         * 标签：label
         * 模板id: templateId
         * 视图容器id: viewId
         */
        function selectTemplate(data, name, label, templateId, viewId) {
            let list = {"name": name, "label": label,"list":data};
            let getTpl = document.getElementById('' + templateId + '').innerHTML;
            let view = document.getElementById('' + viewId + '');
            laytpl(getTpl).render(list, function (html) {
                view.innerHTML = html;
            });
            form.render('select');
        }

        $(document).ready(function () {
            let departments = [[${departments}]];
            let department = [[${department}]];

            console.log(departments);
            // let getTpl = document.getElementById('DepartmentTemplate').innerHTML;
            // let view = document.getElementById('DepartmentView');
            // laytpl(getTpl).render(departments, function(html){
            //     view.innerHTML = html;
            // });
            selectTemplate(departments, "parent", "上级部门", "DepartmentTemplate", "DepartmentView");
            if (department !==null){
                $("#name").val(department.name);
                $('#address').val(department.address);
                $('#situation').val(department.situation);
                $('#principal').val(department.principal);
                $('#telnum').val(department.telnum);
                $('#parent').val(department.parent);
            }
        });
        //监听提交
        form.on('submit(saveBtn)', function (data) {
            $.ajax({
                url:`/addDepartment`,
                type:`post`,
                dataType:`json`,
                data: data.field,
                success:function (res) {
                    console.log(res);
                    if (res.code == 200){
                        layer.msg('添加成功');
                        // let index = parent.layer.getFrameIndex(window.name);
                        // parent.layer.close(index);//关闭当前页
                        layer.close(layer.index);
                        window.parent.location.reload();
                    }else{
                        layer.msg('添加失败');
                    }
                }
            });
            return false;
        });

        window.back = function () {
            let iframeIndex = parent.layer.getFrameIndex(window.name);
            parent.layer.close(iframeIndex);
        }
    });
</script>
</body>
</html>